class Enlarge {
    constructor(el) {
        // 保护全局信息
        this.el = document.querySelector(el)
        this.imgObj = this.el.querySelector('.img')
        this.maskObj = this.el.querySelector('.mask')
        this.enlargeObj = this.el.querySelector('.enlarge')


        // 默认调用
        this.init()
    }

    init() {
        this.overOut()
        this.enlargeSetWH()
        this.maskMove()
        this.smallImgObjs()
    }

    enlargeSetBg(left, top) {
        // 放大镜的宽高(parseFloat获取小数)
        let enlargeW = parseFloat(this.getStyle(this.enlargeObj, 'width'))
        let enlargeH = parseFloat(this.getStyle(this.enlargeObj, 'height'))

        // 遮罩框的宽高（parseInt 去除px）
        let maskW = parseInt(this.getStyle(this.maskObj, 'width'))
        let maskH = parseInt(this.getStyle(this.maskObj, 'height'))

        let x = (enlargeW / maskW) * left
        let y = (enlargeH / maskH) * top

        this.enlargeObj.style.backgroundPosition = `-${x}px -${y}px`
    }

    maskMove() {
        // 1 给img盒模型绑定鼠标移动事件
        // this.imgObj.onmousemove = function(evt) {
        this.imgObj.onmousemove = (evt) => {

            // 2 事件处理函数中
            // 2.1 同步mask遮罩框的left和top - 获取事件对象
            let e = evt || window.event
            // 2.2 同步mask遮罩框的left和top - 计算left和top  公式鼠标的pageX - imgObj.offsetLeft  居中
            let left = e.pageX - this.imgObj.parentNode.offsetLeft - this.maskObj.offsetWidth / 2
            let top = e.pageY - this.imgObj.parentNode.offsetTop - this.maskObj.offsetHeight / 2
            // 2.3 同步mask遮罩框的left和top - 范围判断
            if (left < 0) left = 0
            if (top < 0) top = 0
            let maxLeft = this.imgObj.offsetWidth - this.maskObj.offsetWidth
            let maxTop = this.imgObj.offsetHeight - this.maskObj.offsetHeight
            if (left > maxLeft) left = maxLeft
            if (top > maxTop) top = maxTop
            // 2.4 同步mask遮罩框的left和top - 设置left和top
            this.maskObj.style.left = left + 'px'
            this.maskObj.style.top = top + 'px'

            // 同步
            this.enlargeSetBg(left, top)
        }
    }
    getStyle(tagObj, attrName) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(tagObj)[attrName]
        } else {
            return tagObj.currentStyle[attrName]
        }
    }

    enlargeSetWH() {
        let maskW = parseInt(this.getStyle(this.maskObj, 'width'))
        let maskH = parseInt(this.getStyle(this.maskObj, 'height'))

        let enlarge = this.getStyle(this.enlargeObj, 'background-size')
        let enlargeW = parseInt(enlarge.split(' ')[0])
        let enlargeH = parseInt(enlarge.split(' ')[1])

        let imgW = this.imgObj.clientWidth
        let imgH = this.imgObj.clientHeight

        this.enlargeObj.style.width = maskW * (enlargeW / imgW) + 'px'
        this.enlargeObj.style.height = maskH * (enlargeH / imgH) + 'px'

    }

    overOut() {
        this.imgObj.onmouseover = () => this.maskObj.style.display = this.enlargeObj.style.display = 'block'
        this.imgObj.onmouseout = () => this.maskObj.style.display = this.enlargeObj.style.display = 'none'
    }

    smallImgObjs() {
        // 1. 获取所有小图 遍历绑定 点击事件
        // 2. 事件处理函数中
        //    2.1 移除所有class 给自己加
        //    2.2 大图换
        //    2.3 放大镜换
        let liObjs = this.el.querySelectorAll('ul li')
        liObjs.forEach((item, i) => {
            item.onclick = () => {
                this.el.querySelector('ul li.active').className = ''
                item.className = 'active'
                this.imgObj.style.backgroundImage = `url(./imgs/${i + 1}.jpg)`
                this.enlargeObj.style.backgroundImage = `url(./imgs/${i + 1}.big.jpg)`
            }
        })
    }
}



